﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace IndoorInformation
{
    public partial class frmEditPhongBan : Form
    {
        protected DoiTuong _DoiTuong = null;
        protected EditState _EditState = EditState.None;
        public frmEditPhongBan(Room room)
        {
            InitializeComponent();
            _DoiTuong = room;
        }

        private void frmEditPhongBan_Load(object sender, EventArgs e)
        {
            try
            {
                txtName.Select();
                if (_DoiTuong == null)
                {
                    _DoiTuong = DanhMucRoom.GetAll().NewDoiTuong();
                    _EditState = EditState.CreatingNew;
                }
                else
                {
                    txtName.Text = (_DoiTuong as Room).Name;
                    try
                    {
                        txtX1.Text = (_DoiTuong as Room).X1.ToString();
                    }
                    catch (Exception) { }
                    try
                    {
                        txtX2.Text = (_DoiTuong as Room).X2.ToString();
                    }
                    catch (Exception) { }
                    try
                    {
                        txtX3.Text = (_DoiTuong as Room).X3.ToString();
                    }
                    catch (Exception) { }
                    try
                    {
                        txtX4.Text = (_DoiTuong as Room).X4.ToString();
                    }
                    catch (Exception) { }

                    try
                    {
                        txtY1.Text = (_DoiTuong as Room).Y1.ToString();
                    }
                    catch (Exception) { }
                    try
                    {
                        txtY2.Text = (_DoiTuong as Room).Y2.ToString();
                    }
                    catch (Exception) { }
                    try
                    {
                        txtY3.Text = (_DoiTuong as Room).Y3.ToString();
                    }
                    catch (Exception) { }
                    try
                    {
                        txtY4.Text = (_DoiTuong as Room).Y4.ToString();
                    }
                    catch (Exception) { }

                    _EditState = EditState.Modifying;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                //Check infor

                //Lưu
                (_DoiTuong as Room).Name = txtName.Text;
                try
                {
                    (_DoiTuong as Room).X1 = Convert.ToDouble(txtX1.Text.Trim());
                }
                catch (Exception) { }
                try
                {
                    (_DoiTuong as Room).X2 = Convert.ToDouble(txtX2.Text.Trim());
                }
                catch (Exception) { }
                try
                {
                    (_DoiTuong as Room).X3 = Convert.ToDouble(txtX3.Text.Trim());
                }
                catch (Exception) { }
                try
                {
                    (_DoiTuong as Room).X4 = Convert.ToDouble(txtX4.Text.Trim());
                }
                catch (Exception) { }

                try
                {
                    (_DoiTuong as Room).Y1 = Convert.ToDouble(txtY1.Text.Trim());
                }
                catch (Exception) { }
                try
                {
                    (_DoiTuong as Room).Y2 = Convert.ToDouble(txtY2.Text.Trim());
                }
                catch (Exception) { }
                try
                {
                    (_DoiTuong as Room).Y3 = Convert.ToDouble(txtY3.Text.Trim());
                }
                catch (Exception) { }
                try
                {
                    (_DoiTuong as Room).Y4 = Convert.ToDouble(txtY4.Text.Trim());
                }
                catch (Exception) { }

                if (_EditState == EditState.CreatingNew)
                {
                    (_DoiTuong as Room).InsertToDB();
                }
                else
                {
                    (_DoiTuong as Room).UpdateToDB();
                }

                MessageBox.Show("Lưu thành công");
                this.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
